package com.midou.administration.controller;

import cn.hutool.core.date.DateTime;
import com.midou.administration.bo.Msg;
import com.midou.administration.entity.Coupon;
import com.midou.administration.entity.UserCoupon;
import com.midou.administration.service.CouponService;
import com.midou.administration.service.RedisService;
import com.midou.administration.service.UserCouponService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.xml.crypto.Data;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
@RequestMapping("/administration/redis")
public class RedisController {

    @Autowired
    private RedisService redisService;

    @Autowired
    private CouponService couponService;

    @RequestMapping("/qiangou")
    public Msg qianggou(@RequestBody UserCoupon userCoupon){
        /*判断用户是否抢购过该商品*/
        boolean b = this.redisService.Redo(userCoupon.getUcCpId(),userCoupon.getUcUserId());
        System.out.println("用户是否抢购过该商品:"+b);
        System.out.println("该商品的活动id是:" + userCoupon.getUcCpId() + "\n用户id是:"+userCoupon.getUcUserId());
        if(b){
            System.out.println("参加过该活动了!");
            return new Msg(false,"您已经参加过该活动!请看一下其他商家活动信息!");
        }
        /* 判断活动是否开始  结束 */
        Coupon coupon = this.couponService.selectByid(userCoupon.getUcCpId());

        /*比对时间*/
        long newDate = convert(new DateTime());
        long begin  = convert(coupon.getCpBeginTime());
        long end = convert(coupon.getCpEndTime());
        if(newDate>begin&&newDate>end){
            return new Msg(false,"活动已经结束");
        }
        if(newDate<begin){
            return new Msg(false,"活动还未开始");
        }
        userCoupon.setUcBId(coupon.getBuId());
        //优惠券给商家赋值
//        //正常抢购
        return this.redisService.getResule(userCoupon);
    }

    public long convert(Date date){
        long ts = date.getTime();
        ts = ts/1000;
        System.out.println(ts);
        return ts;
    }








}
